package com.lyj.mall.controller;

import com.lyj.mall.pojo.LogInfo;
import com.lyj.mall.pojo.User;
import com.lyj.mall.service.LogInfoService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;


/**
 * @author lyj
 * @date 2020/1/11 11:08
 */
public class BaseController {

    @Autowired
    private LogInfoService logInfoService;

    public User user = getUser();

    public User getUser() {
        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getPrincipal();
        if (null == user) {
            user = new User();
            user.setUsername("unknow");
        }
        return user;
    }

    public void insertLog(String content, String logType, String params) {
        LogInfo logInfo = new LogInfo();
        logInfo.setContent(content);
        logInfo.setLogType(logType);
        logInfo.setParams(params);
        logInfo.setOperator(this.getUser().getUsername());
        logInfoService.save(logInfo);
    }
}
